
** South Africa
** Clean Wave 4

*************
*************

	** ROSTER
		use "$dir/rawdata/NIDS South Africa/wave4/HouseholdRoster_W4_Anon_V2.0.0.dta", clear
		gen w4_inhh=w4_r_pres==1 if !missing(w4_r_pres)
					
	** Household size
		gen temp=1 if w4_inhh==1
		bys w4_hhid: egen w4_hh_size=total(temp)
		drop temp
		
	** Marital status
		gen w4_married=w4_r_mar==1 if !missing(w4_r_mar)
		replace w4_married=0 if w4_r_noparh==3 | w4_r_noparh==4 | w4_r_noparh==5

	** Save
		keep w4_hhid pid w4_inhh w4_hh_size w4_married
		tempfile roster
		save `roster', replace
		
*************
*************

	** INDIVIDUAL FILES (DERIVED)
		use "$dir/rawdata/NIDS South Africa/wave4/indderived_W4_Anon_V2.0.0.dta", clear
		
	** Female
		gen w4_female=w4_best_gen==2 if !missing(w4_best_gen) & w4_best_gen>0

	** Age
		gen w4_age=w4_best_age_yrs
		
	** Works
		gen w4_work_any_c=w4_empl_stat==3 if !missing(w4_empl_stat)
		
		keep w4_hhid pid w4_age w4_work_any_c w4_female
		tempfile indderiv 
		save `indderiv', replace
			 
	** INDIVIDUAL FILES
		use "$dir/rawdata/NIDS South Africa/wave4/Adult_W4_Anon_V2.0.0.dta", clear
		
	** Employment
		gen w4_work_primaryocc=w4_a_em1==1 if !missing(w4_a_em1) & w4_a_em1>0
		gen w4_work_secondaryocc=w4_a_em2==1 if !missing(w4_a_em2) & w4_a_em2>0
		gen w4_work_selfemp=w4_a_ems==1 if !missing(w4_a_ems) & w4_a_ems>0
		gen w4_work_casual=w4_a_emc==1 if !missing(w4_a_emc) & w4_a_emc>0
		gen w4_work_agri=w4_a_emp==1 if !missing(w4_a_emp) & w4_a_emp>0
		gen w4_work_bizhelp=w4_a_emh==1 if !missing(w4_a_emh) & w4_a_emh>0
		
		egen w4_work_any=rowmax(w4_work_primaryocc w4_work_secondaryocc w4_work_selfemp w4_work_casual w4_work_agri w4_work_bizhelp)

	** Work hours
		foreach var in w4_a_em1hrs w4_a_em2hrs w4_a_emshrs w4_a_emchrs w4_a_emphrs w4_a_emhhrs {
			replace `var'=. if `var'<0
		}
		egen w4_work_hrs=rowtotal(w4_a_em1hrs w4_a_em2hrs w4_a_emshrs w4_a_emchrs w4_a_emphrs w4_a_emhhrs)
	
	** CESD
		foreach var in w4_a_emobth w4_a_emomnd w4_a_emodep w4_a_emoeff w4_a_emohope w4_a_emofear w4_a_emoslp w4_a_emohap w4_a_emolone w4_a_emogo {
			recode `var' -8=. -3=. -9=.
			recode `var' 1=0 2=1 3=2 4=3
		}
		replace w4_a_emohope=3-w4_a_emohope
		replace w4_a_emohap=3-w4_a_emohap
		egen w4_cesd_total=rowtotal(w4_a_emobth w4_a_emomnd w4_a_emodep w4_a_emoeff w4_a_emohope w4_a_emofear w4_a_emoslp w4_a_emohap w4_a_emolone w4_a_emogo)
		egen w4_cesd_count=rownonmiss(w4_a_emobth w4_a_emomnd w4_a_emodep w4_a_emoeff w4_a_emohope w4_a_emofear w4_a_emoslp w4_a_emohap w4_a_emolone w4_a_emogo)
		replace w4_cesd_total=. if w4_cesd_count<8 // don't score if more than 2 are missing
		
		gen w4_depressed=w4_cesd_total>=10
		
	** DISABILITIES
		tab1 w4_a_hlvisaid w4_a_hlhraid 
		tab1 w4_a_hlvisaid w4_a_hlhraid, nol

	** Difficulty seeing
		gen w4_diff_seei = w4_a_hlvisaid
		replace w4_diff_seei = 0 if w4_a_hlvisaid==2
		replace w4_diff_seei = . if w4_a_hlvisaid < 0
		tab1 w4_diff_seei

	** Difficulty hearing
		gen w4_diff_hear = w4_a_hlhraid
		replace w4_diff_hear = 0 if w4_a_hlhraid==2
		replace w4_diff_hear = . if w4_a_hlhraid < 0
		tab1 w4_diff_hear

	** Difficulty walking or climbing stairs

		*na


	** Difficulty remembering or concentrating
		*na

	** Difficulty with self-care

		*na

	** Difficulty communicating

		*na

		keep w4_hhid pid w4_work_any w4_work_hrs w4_work_primaryocc w4_work_secondaryocc w4_work_selfemp w4_work_casual w4_work_agri w4_work_bizhelp w4_cesd_count w4_depressed w4_diff_seei w4_diff_hear
		tempfile indiv
		save `indiv', replace
		
*************
*************

	** HOUSEHOLD DATA 

		use "$dir/rawdata/NIDS South Africa/wave4/hhderived_W4_Anon_V2.0.0.dta", clear
		
		gen w4_urban=w4_geo2011==2 if !missing(w4_geo2011)
		gen w4_hh_size_check=w4_hhsizer
		keep w4_hhid w4_hh_size_check w4_urban
			
		tempfile hh
		save `hh', replace
		
*************
*************

	** APPEND
		
		use `hh', clear
		merge 1:m w4_hhid using `roster', assert(matched) nogen
		merge 1:1 w4_hhid pid using `indderiv', assert(master matched) gen(merge_indderiv)
		merge 1:1 w4_hhid pid using `indiv', assert(master matched) gen(merge_indiv)
		
		order w4_hhid pid
		drop merge*
		
		compare w4_work_any_c w4_work_any
		drop w4_work_any_c
		compare w4_hh_size_check w4_hh_size
		drop w4_hh_size_check
		
		save "$dir/data/saf_w4_individual.dta", replace
		
	** Missing variables: 
	** Urban status
	
	** Illness and injury

	** Impact of illness and injury
		